home *** CD-ROM | disk | FTP | other *** search
/ Aminet 39 / Aminet 39 (2000)(Schatztruhe)[!][Oct 2000].iso / Aminet / game / shoot / Orbit_src.lha / Orbit / source / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-07-04  |  5.0 KB  |  256 lines

  1. /*
  2.     Amiga port by Oliver Gantert
  3.  
  4.     27.04.2000 - fixed some compiler warnings
  5. */
  6. /*
  7.  
  8. ORBIT, a freeware space combat simulator
  9. Copyright (C) 1999  Steve Belczyk <steve1@genesis.nred.ma.us>
  10.  
  11. This program is free software; you can redistribute it and/or
  12. modify it under the terms of the GNU General Public License
  13. as published by the Free Software Foundation; either version 2
  14. of the License, or (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  24.  
  25. */
  26.  
  27. #include "orbit.h"
  28. #include <stdarg.h>
  29.  
  30. void InitConsole()
  31. /*
  32.  *  Initialize the message console
  33.  */
  34. {
  35.   int i;
  36.  
  37.   console.next = 0;
  38.  
  39.   for (i=0; i<CONSLINES; i++)
  40.   {
  41.     console.buf[i][0] = 0;
  42.     console.age[i] = 0.0;
  43.   }
  44. }
  45.  
  46. void Cprint (char *c, ...)
  47. /*
  48.  *  Print a message to the console
  49.  */
  50. {
  51.   int i;
  52.   va_list ap;
  53.   char buf[256];
  54.  
  55.   /* Don't bother if NULL */
  56.   if (c == NULL) return;
  57.  
  58.   va_start (ap, c);
  59.   vsprintf (buf, c, ap);
  60.   va_end (ap); 
  61.  
  62.   /* Scroll messages if at end */
  63.   if (console.next == CONSLINES)
  64.   {
  65.     for (i=0; i<CONSLINES-1; i++)
  66.     {
  67.       strcpy (console.buf[i], console.buf[i+1]);
  68.       console.age[i] = console.age[i+1];
  69.     }
  70.     console.next--;
  71.   }
  72.  
  73.   /* Add this line to buffer */
  74.   strcpy (console.buf[console.next], buf);
  75.   console.age[console.next] = 0.0;
  76.   console.next++;
  77. }
  78.  
  79. void DisplayConsole()
  80. /*
  81.  *  Display the console messages
  82.  */
  83. {
  84.   int i, y;
  85.  
  86.   /* Bump age */
  87.   for (i=0; i<console.next; i++) console.age[i] += deltaT;
  88.  
  89.   /* Else display each line */
  90.   y = ScreenHeight - CONSHEIGHT;
  91.   for (i=0; i<console.next; i++)
  92.   {
  93.     if (console.age[i] < CONSAGE)
  94.     {
  95.       glColor3f (0.0, 0.8, 0.2);
  96.       glRasterPos2i (1, y);
  97.       Print (GLUT_BITMAP_HELVETICA_10, console.buf[i]);
  98.       y -= CONSHEIGHT;
  99.     }
  100.   }
  101. }
  102.  
  103. void InitMessage()
  104. /*
  105.  *  Initialize the message system
  106.  */
  107. {
  108.   strcpy (message.text, "No message");
  109.   message.len = glutBitmapLength (GLUT_BITMAP_HELVETICA_10,
  110.   message.text);
  111.   message.age = MSG_MAXAGE + 1.0;
  112. }
  113.  
  114. void DrawMessage()
  115. /*
  116.  *  Draw the message on the screen
  117.  */
  118. {
  119.   int rows, x, y, wrap, pixels_per_row;
  120.   char *p;
  121.  
  122.   /* Bump age */
  123.   if (!text.yes) message.age += deltaT;
  124.  
  125.   /* Never mind if too old */
  126.   if (message.age > MSG_MAXAGE) return;
  127.  
  128.   wrap = 0;
  129.   pixels_per_row = 3 * ScreenWidth / 4;
  130.  
  131.   /* Guess out how many rows this message will take */
  132.   rows = message.len / pixels_per_row;
  133.  
  134.   /* Figure out where to start */
  135.   y = (ScreenHeight / 2) + (CONSHEIGHT / 2) * (rows + 1);
  136.   if (rows == 0)
  137.   {
  138.     /* Center if short */
  139.     x = (ScreenWidth - message.len) / 2;
  140.   }
  141.   else
  142.   {
  143.     /* Start at left margin */
  144.     x = ScreenWidth / 8;
  145.   }
  146.  
  147.   /* Display the message */
  148.   glColor3f (1.0, 0.5, 0.0);
  149.   glRasterPos2i (x, y);
  150.   p = message.text;
  151.   while (*p)
  152.   {
  153.     /* Look for newline */
  154.     if (*p == '\\')
  155.     {
  156.       wrap = 0;
  157.       x = ScreenWidth / 8;
  158.       y -= CONSHEIGHT;
  159.       glRasterPos2i (x, y);
  160.       p++;
  161.       continue;
  162.     }
  163.  
  164.     /* Break at space */
  165.     if (wrap && (*p == ' '))
  166.     {
  167.       x = ScreenWidth / 8;
  168.       y -= CONSHEIGHT;
  169.       glRasterPos2i (x, y);
  170.       wrap = 0;
  171.     }
  172.     glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, *p);
  173.     x += glutBitmapWidth (GLUT_BITMAP_HELVETICA_10, *p);
  174.     p++;
  175.     if (x >= (pixels_per_row + ScreenWidth/8)) wrap = 1;
  176.   }
  177. }
  178.  
  179. void Mprint (char *msg, ...)
  180. /*
  181.  *  Print a message in the center of the screen
  182.  */
  183. {
  184.   char buf[4096];
  185.   int pixels_per_row;
  186.   unsigned int i;
  187.   va_list ap;
  188.  
  189.   if (msg == NULL) return;
  190.  
  191.   va_start (ap, msg);
  192.   vsprintf (buf, msg, ap);
  193.   va_end (ap); 
  194.  
  195.   pixels_per_row = 3 * ScreenWidth / 4;
  196.   
  197.   message.age = 0.0;
  198.   strcpy (message.text, buf);
  199.   message.len = glutBitmapLength (GLUT_BITMAP_HELVETICA_10,
  200.   message.text);
  201.  
  202.   /* Fudge length upward for each newline */
  203.   for (i=0; i<strlen(buf); i++)
  204.   {
  205.     /* Guess that each newline will add half a row to length */
  206.     if (msg[i] == '\\') message.len += pixels_per_row / 2;
  207.   }
  208.  
  209.   /* Give the sound */
  210.   if (sound && !text.yes) PlayAudio (SOUND_COMM);
  211. }
  212.  
  213. void DoChat ()
  214. /*
  215.  *  Process chat buffer
  216.  */
  217. {
  218.   int c;
  219.  
  220.   /* Show message to us */
  221.   Mprint (" ");
  222.   Cprint ("%s: %s", player.name, text.buf);
  223.   Log ("DoChat: %s: %s", player.name, text.buf);
  224.  
  225.   if (am_client)
  226.   {
  227.     /* Send to server */
  228.     SendASCIIPacket (clientme.socket, "CHAT %s", text.buf);
  229.   }
  230.   else if (am_server)
  231.   {
  232.     /* Send to each active client */
  233.     for (c=0; c<NCLIENTS; c++)
  234.     {
  235.       if (client[c].active && (c != server.client))
  236.       {
  237.         SendASCIIPacket (client[c].socket, "CMSG %s: %s",
  238.         player.name, text.buf);
  239.       }
  240.     }
  241.   }
  242. }
  243.  
  244. void GetText (char *prompt, void (*func)(void))
  245. /*
  246.  *  Start getting text from player
  247.  */
  248. {
  249.   text.yes = 1;
  250.   text.index = 0;
  251.   text.buf[0] = 0;
  252.   strcpy (text.prompt, prompt);
  253.   text.func = func;
  254.   Mprint ("%s", text.prompt);
  255. }
  256.